package com.itbaiwei.unitday01;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;

import java.util.List;
/**
 * Class: MyRecycleAdapter
 * Description:
 * Author: haijun.xu
 * Date: 2025/4/28 20:19
 */
public class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.MyViewHolder> {

    private Context context;
    private List<String> list;

    public int width=240;
    public int height=320;


    public MyRecycleAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_a, null);
        ViewGroup.LayoutParams layoutParams = parent.getLayoutParams();
        LinearLayout.LayoutParams layoutParams1=new LinearLayout.LayoutParams(width,height);
        if(layoutParams instanceof ViewGroup.MarginLayoutParams){
            //注意这个方法获取出来的是px,而非dp
            int leftMargin = ((ViewGroup.MarginLayoutParams) layoutParams).leftMargin;
            float left = pxToDp(context, leftMargin);
            int rightMargin = ((ViewGroup.MarginLayoutParams) layoutParams).rightMargin;
            float right = pxToDp(context, rightMargin);
           // System.out.println("leftMargin"+left);
           // System.out.println("rightMargin"+right);
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
           int screenWidth = metrics.widthPixels-leftMargin-rightMargin-40; // 屏幕宽度（像素值，px）
             width=  screenWidth/3;//控件的宽度
            float a=1.35f;
             height= (int) (width*a);
            System.out.println("宽度是："+width);
            System.out.println("高度是："+height);
             layoutParams1=new LinearLayout.LayoutParams(width,height);

        }


        return new MyViewHolder(view,layoutParams1);
    }

    @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {

        System.out.println("记载图片地址是："+list.get(position));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"点击的位置："+position,Toast.LENGTH_LONG).show();
            }
        });

        //holder.iv_icon.setImageResource();
        //.transform(new RoundedCorners(20))
        //new CenterCrop(), new RoundedCorners(20)
//        RequestOptions options = new RequestOptions()
//                .placeholder(R.drawable.common_ui_serach_cover_bg)  // 加载中显示
//                .error(R.drawable.common_ui_serach_cover_bg)
//                .skipMemoryCache(true)  // 禁用内存缓存
//                .diskCacheStrategy(DiskCacheStrategy.NONE);  // 禁用磁盘缓存;
//        Glide.with(context).load(list.get(position)).apply(options).into(holder.iv_icon);
//
        holder.iv_icon.setImageResource(R.drawable.common_ui_serach_cover_bg);

// 延迟 2 秒后再加载图片（可根据需要调整时间）
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            Glide.with(context)
                    .load(list.get(position))
                    .apply(new RequestOptions()
                            .placeholder(R.drawable.common_ui_serach_cover_bg)
                            .error(R.drawable.common_ui_serach_cover_bg)
                    )
                    .into(holder.iv_icon);
        }, 0);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        public ImageView iv_icon;

        public MyViewHolder(@NonNull View itemView,  ViewGroup.LayoutParams params) {

            super(itemView);
            iv_icon=itemView.findViewById(R.id.iv_icon);
            // 动态设置 ImageView 宽高为 item 的 100%
//            ViewGroup.LayoutParams paramsa = new ViewGroup.LayoutParams(
//                    ViewGroup.LayoutParams.MATCH_PARENT,
//                   400
//            );
            iv_icon.setLayoutParams(params);
            // 动态设置 ImageView 宽高为 item 的 100%
           // iv_icon.setLayoutParams(params);
        }


    }
    /**
     * 将 px 值转换为 dp 值
     * @param pxValue 像素值
     * @return dp 值
     */
    public static float pxToDp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return pxValue / scale;
    }


}
